package com.nami.conver.infra.gateway;

import cn.hutool.core.collection.CollUtil;
import com.nami.basic.dto.MultiResponse;
import com.nami.conver.domain.gateway.GrpMemberGateWay;
import com.nami.conver.infra.cache.member.GrpMembersCache;
import com.nami.gchat.client.api.GrpMemberClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class GrpMemberGateWayImpl implements GrpMemberGateWay {

    @DubboReference
    GrpMemberClient grpMemberClient;

    @Override
    public List<String> findMemberUserIdsByGrpId(String grpId) {
        List<String> userIds = GrpMembersCache.get(grpId);
        if (CollUtil.isEmpty(userIds)) {
            MultiResponse<String> response = grpMemberClient.listOfUserIdsByGrpMemberQry(grpId);
            if (!response.isSuccess() || CollUtil.isEmpty(response.getData())) {
                return null;
            }

            List<String> grpMembers = response.getData();
            if (CollUtil.isEmpty(grpMembers)) {
                log.warn("[grp-member] 获取群成员用户id为空，grpId={}", grpId);
                return null;
            }
            GrpMembersCache.add(grpId, grpMembers);
            return new ArrayList<>(grpMembers);
        }
        return userIds;
    }


}
